package com.shawn.spring.boot.mongo;

import com.mongodb.client.result.UpdateResult;
import com.shawn.spring.boot.mongo.dao.UserDao;
import com.shawn.spring.boot.mongo.entity.User;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.domain.Example;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

/**
 * @author shawn
 * @since 2020/8/26
 */
@SpringBootApplication
@Slf4j
public class MongoApplication implements ApplicationRunner {

  @Autowired private UserDao userDao;

  @Autowired private MongoTemplate mongoTemplate;

  public static void main(String[] args) {
    SpringApplication.run(MongoApplication.class, args);
  }

  @Override
  public void run(ApplicationArguments args) throws Exception {
    User user =
        User.builder()
            .id("1")
            .username("shawn")
            .password("shawn")
            .createTime(new Date())
            .updateTime(new Date())
            .build();
    userDao.insert(user);
    log.info("user {}", user);

    Optional<User> optional = userDao.findById("1");
    log.info("find by id user {}", optional.get());

    List<User> list = userDao.findAll();
    list.forEach(e -> log.info("findAll User: {}", e));

    User condition = User.builder().username("shawn").build();
    List<User> userList = userDao.findAll(Example.of(condition));
    userList.forEach(e -> log.info("findAll User by Example: {}", e));
    Thread.sleep(1000); // 为了看更新时间
    user.setPassword("123456");
    user.setUpdateTime(new Date());
    userDao.save(user);
    log.info("find by id user {}", userDao.findById("1").get());

    userDao.deleteById("1");
    userDao.findById("1").ifPresent(u -> log.info("find by id user {}", u));

    User user2 =
        User.builder()
            .id("2")
            .username("jack")
            .password("jack")
            .createTime(new Date())
            .updateTime(new Date())
            .build();
    mongoTemplate.save(user2);
    log.info("User2 {}", user2);

    List<User> user2List =
        mongoTemplate.find(Query.query(Criteria.where("username").is("jack")), User.class);
    user2List.forEach(c -> log.info("User2 {}", c));

    Thread.sleep(1000); // 为了看更新时间
    UpdateResult result =
        mongoTemplate.updateFirst(
            Query.query(Criteria.where("username").is("jack")),
            new Update().set("password", "jack12346").currentDate("updateTime"),
            User.class);
    log.info("Update Result: {}", result.getModifiedCount());
    User updateOne = mongoTemplate.findById(user2.getId(), User.class);
    log.info("Update Result: {}", updateOne);

    mongoTemplate.remove(updateOne);
    log.info("Update Result: {}", mongoTemplate.findById(user2.getId(), User.class));
  }
}
